home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / system / cache.php < prev    next >
PHP Script  |  2008-07-06  |  3KB  |  115 lines

  1. <?php
  2. /**
  3. * @version        $Id: cache.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package        Joomla
  5. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license        GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. jimport( 'joomla.plugin.plugin' );
  18.  
  19. /**
  20.  * Joomla! Page Cache Plugin
  21.  *
  22.  * @author        Johan Janssens <johan.janssens@joomla.org>
  23.  * @package        Joomla
  24.  * @subpackage    System
  25.  */
  26. class  plgSystemCache extends JPlugin
  27. {
  28.  
  29.     var $_cache = null;
  30.  
  31.     /**
  32.      * Constructor
  33.      *
  34.      * For php4 compatability we must not use the __constructor as a constructor for plugins
  35.      * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  36.      * This causes problems with cross-referencing necessary for the observer design pattern.
  37.      *
  38.      * @access    protected
  39.      * @param    object    $subject The object to observe
  40.      * @param     array   $config  An array that holds the plugin configuration
  41.      * @since    1.0
  42.      */
  43.     function plgSystemCache(& $subject, $config)
  44.     {
  45.         parent::__construct($subject, $config);
  46.  
  47.         $user =& JFactory::getUser();
  48.  
  49.         $options = array(
  50.             'cachebase'     => JPATH_BASE.DS.'cache',
  51.             'defaultgroup'     => 'page',
  52.             'lifetime'         => $this->params->get('cachetime', 15) * 60,
  53.             'browsercache'    => $this->params->get('browsercache', false),
  54.             'caching'        => false
  55.         );
  56.  
  57.         jimport('joomla.cache.cache');
  58.         $this->_cache =& JCache::getInstance( 'page', $options );
  59.  
  60.         if (!$user->get('aid') && $_SERVER['REQUEST_METHOD'] == 'GET') {
  61.             $this->_cache->setCaching(true);
  62.         }
  63.     }
  64.  
  65.     /**
  66.     * Converting the site URL to fit to the HTTP request
  67.     *
  68.     */
  69.     function onAfterInitialise()
  70.     {
  71.         global $mainframe, $_PROFILER;
  72.  
  73.          if($mainframe->isAdmin() || JDEBUG) {
  74.              return;
  75.          }
  76.  
  77.  
  78.         $data  = $this->_cache->get();
  79.  
  80.         if($data !== false)
  81.         {
  82.             // the following code searches for a token in the cached page and replaces it with the
  83.             // proper token.
  84.             $user    = &JFactory::getUser();
  85.             $token    = JUtility::getToken();
  86.             $search = '#<input type="hidden" name="[0-9a-f]{32}" value="1" />#';
  87.             $replacement = '<input type="hidden" name="'.$token.'" value="1" />';
  88.             $data = preg_replace( $search, $replacement, $data );
  89.  
  90.             JResponse::setBody($data);
  91.  
  92.             echo JResponse::toString($mainframe->getCfg('gzip'));
  93.  
  94.             if(JDEBUG)
  95.             {
  96.                 $_PROFILER->mark('afterCache');
  97.                 echo implode( '', $_PROFILER->getBuffer());
  98.             }
  99.  
  100.             $mainframe->close();
  101.         }
  102.     }
  103.  
  104.     function onAfterRender()
  105.     {
  106.         global $mainframe;
  107.  
  108.         if($mainframe->isAdmin() || JDEBUG) {
  109.             return;
  110.         }
  111.  
  112.         $this->_cache->store();
  113.     }
  114. }
  115.